#!/bin/bash -e

source $PATH_SCRIPTS/get-package.sh
source $PATH_SCRIPTS/tool-util.sh

# present the header
echo	"


=====================================================================

           Utility to generate level 2 tools
 
=====================================================================



"

set +h
umask 022
LC_ALL=POSIX
LFS_TGT=$(uname -m)-$JAIL_NAME-linux-gnu
PATH=/tools/bin:/bin:/usr/bin
export LC_ALL LFS_TGT PATH MAKEFLAGS




# ===============================================================
initDir "5.8." "binutils-2.22"
	pushd $SRC_DIR > /dev/null
		patch -Np1 -i $PATH_PKGCACHE/binutils-2.22-build_fix-1.patch >> $CRTLOG
	popd > /dev/null


	CC=$LFS_TGT-gcc            \
	AR=$LFS_TGT-ar             \
	RANLIB=$LFS_TGT-ranlib     \
	$SRC_DIR/configure         \
		--prefix=/tools        \
		--disable-nls          \
		--with-lib-path=/tools/lib >> $CRTLOG
    
	make >> $CRTLOG
	make install >> $CRTLOG

	#  Now prepare the linker for the “Re-adjusting” phase in the next chapter: 
	make -C ld clean >> $CRTLOG
	make -C ld LIB_PATH=/usr/lib:/lib >> $CRTLOG
	cp -v ld/ld-new /tools/bin

rDir
# ===============================================================


# ===============================================================
initDir "5.9." "gcc-4.7.2"
	pushd $SRC_DIR


		# GCC now requires the GMP, MPFR and MPC packages
		getCachedPackage "mpfr-2.4.2" $SRC_DIR/mpfr
		getCachedPackage "gmp-4.3.2" $SRC_DIR/gmp
		getCachedPackage "mpc-0.8.1" $SRC_DIR/mpc

		# Create a full version of the internal header using a command that is 
		# identical to what the GCC build system does in normal circumstances:
		cat gcc/limitx.h gcc/glimits.h gcc/limity.h > \
		  `dirname $($LFS_TGT-gcc -print-libgcc-file-name)`/include-fixed/limits.h


		cp -v gcc/Makefile.in{,.tmp}
		sed 's/^T_CFLAGS =$/& -fomit-frame-pointer/' gcc/Makefile.in.tmp \
		  > gcc/Makefile.in
  
  
		# The following command will change the location of 
		# GCC's default dynamic linker to use the one installed 
		# in /tools. It also removes /usr/include from GCC's 
		# include search path
		for file in \
		  $(find gcc/config -name linux64.h -o -name linux.h -o -name sysv4.h)
		do
			cp -uv $file{,.orig}
			sed -e 's@/lib\(64\)\?\(32\)\?/ld@/tools&@g' \
			  -e 's@/usr@/tools@g' $file.orig >> $file
			echo '
#undef STANDARD_STARTFILE_PREFIX_1
#undef STANDARD_STARTFILE_PREFIX_2
#define STANDARD_STARTFILE_PREFIX_1 "/tools/lib/"
#define STANDARD_STARTFILE_PREFIX_2 ""' >> $file
			touch $file.orig
		done

	popd

	CC=$LFS_TGT-gcc                 \
	AR=$LFS_TGT-ar                  \
	RANLIB=$LFS_TGT-ranlib          \
	$SRC_DIR/configure              \
		--prefix=/tools             \
		--with-local-prefix=/tools  \
		--with-native-system-header-dir=/tools/include \
		--enable-clocale=gnu        \
		--enable-shared             \
		--enable-threads=posix      \
		--enable-__cxa_atexit       \
		--enable-languages=c,c++    \
		--disable-libstdcxx-pch     \
		--disable-multilib          \
		--disable-bootstrap         \
		--disable-libgomp >> $CRTLOG
    
    
finishDir
ln -sv gcc /tools/bin/cc
# ===============================================================




# ===============================================================
# CHECKPOINT

cd $JAIL_PATH/build-dir

echo	"


At this point, it is imperative to stop and ensure that the 
basic functions (compiling and linking) of the new 
toolchain are working as expected.

"
echo 'main(){}' > dummy.c
cc dummy.c
readelf -l a.out | grep ': /tools'

echo	"
If everything is working correctly, there should be no errors, 
and the output of the last command will be of the form:
[Requesting program interpreter: /tools/lib/ld-linux.so.2]
Note that /tools/lib, or /tools/lib64 for 64-bit machines appears 
as the prefix of the dynamic linker.

If the output is not shown as above or there was no output at all, 
then something is wrong. Investigate and retrace the steps to
find out where the problem is and correct it. This issue must 
be resolved before continuing on.

Press CTRL+C to stop.
Press RETURN to continue.
"

# Once all is well, clean up the test files:
rm -v dummy.c a.out

read dummy

# CHECKPOINT
# ===============================================================




# present the footer
echo	"


=====================================================================

           Utility to generate level 2 tools done
 
=====================================================================



"
